require(["common", "jQuery", "coAjax", "queryString", "href", "hash_routie"], function(jSouper, $, coAjax, QueryString, href, hash_routie) {
	var _num = 10;
	var _page = 0;

	function _load_goods_page(num, page) {
		coAjax.get(appConfig.goods.list_with_page_info, {
			bus_id: appConfig.bus_id,
			num: num,
			page: page
		}, function(result) {
			var info = result.result;
			console.log("bus.goods", info.goods);
			App.set("goods", info.goods);
			var page_info = {
				total_num: info.total_num,
				total_page: info.total_page,
				page: info.page,
				num: info.num
			};
			page_info._number_list = [];
			page_info._number_list.length = info.total_page;
			App.set("$Cache.bus.goods.page_info", page_info);
		});
	};

	function _init_goods_data() {
		var queryString = new QueryString(location.hash);
		_num = queryString.get("num") || _num;
		_page = queryString.get("page") || _page;
		_load_goods_page(_num, _page);
	};
	//分页功能
	hash_routie.on("goodsmanager", _init_goods_data);

	//删除商品
	App.set("$Event.remove_goods", function(e, vm) {
		myConfirm("确定删除“" + vm.get("goods_name") + "”", function(result) {
			coAjax["delete"](appConfig.bus.remove_goods, {
				goods_id: vm.get("_id"),
				bus_id: appConfig.bus_id
			}, function(result) {
				App.set("goods", result.result);
			});
		}, function() {
			console.log("取消删除");
		});
	});
	//修改商品
	App.set("$Event.update_goods", function(e, vm) {
		App.set("$Cache.goods_to_update", vm.get());
		location.hash = "admin/update_goods?id=" + vm.get("_id");
	});
	//置顶商品
	App.set("$Event.to_top", function(e, vm) {
		coAjax.put(appConfig.bus.update_goods_index, {
			goods_id: vm.get("_id"),
			goods_index: 0
		}, function() {
			_init_goods_data();
		})
	});
	App.set("$Event.to_up", function(e, vm) {
		coAjax.put(appConfig.bus.update_goods_index, {
			goods_id: vm.get("_id"),
			goods_index: (~~vm.get("$Index") + _num * _page) - 1
		}, function() {
			_init_goods_data();
		})
	});
	App.set("$Event.to_down", function(e, vm) {
		coAjax.put(appConfig.bus.update_goods_index, {
			goods_id: vm.get("_id"),
			goods_index: (~~vm.get("$Index") + _num * _page) + 1
		}, function() {
			_init_goods_data();
		})
	});
});